package com.android.widget.imgtxtimg;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.List;

import com.android.widget.demo.R;

public class MyListViewAdapter extends  ArrayAdapter<ImgTxtImg> {
    private Context mContext;
    private int mResource;

    public MyListViewAdapter(Context context, int resource,List<ImgTxtImg> items) {
        super(context,resource,items);
        mContext = context;
        mResource = resource;
    }       

    @Override
    public View getView(final int position,View convertView,ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource,null);
            
            viewHolder = new ViewHolder();
            viewHolder.imageLeft = (ImageView) convertView.findViewById(R.id.img_left);
            viewHolder.text = (TextView) convertView.findViewById(R.id.text_middle);
            viewHolder.imageRight = (ImageView) convertView.findViewById(R.id.img_right);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();            
        }

        ImgTxtImg item = getItem(position);
        viewHolder.text.setText(item.txt);
        viewHolder.imageLeft.setImageResource(item.url1);
        viewHolder.imageRight.setImageResource(item.url2);
        viewHolder.imageRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext,"image click, position: "+ position,
                        Toast.LENGTH_LONG).show();
 
            }
        });
        return convertView;
    }
    
    private static class ViewHolder {
        ImageView imageLeft;
        TextView text;
        ImageView imageRight;
    }
}
